package com.devfest.wishmaster.gui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.devfest.wishmaster.R;
import com.devfest.wishmaster.model.Comment;
import com.devfest.wishmaster.model.WishItem;
import com.devfest.wishmaster.model.WishList;

public class CommentsActivity extends Activity {

	private CommentsAdapter commentsAdapter;
	private EditText commentEditText;
	private WishItem wishItem;
	private ViewHolder holder;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comments);

		int wishListPosition = getIntent().getIntExtra("wish_list_position", 0);
		int wishItemPosition = getIntent().getIntExtra("wish_item_position", 0);
		List<WishList> wishlists = MyApplication.getInstance().wishLists;
		wishItem = wishlists.get(wishListPosition).getWishItems().get(wishItemPosition);
		
		setItemInfo();
		
		commentsAdapter = new CommentsAdapter(this, wishItem.getNewComments());
		ListView commentsListView = (ListView) findViewById(R.id.comments);
		commentEditText = (EditText) findViewById(R.id.comment);
		findViewById(R.id.add_comment_button).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View view) {
						String text = commentEditText.getText().toString();
						if (text.trim().equals("")) {
							return;
						}
						Comment comment = new Comment();
						comment.setDatecreated(new Date().toString());
						comment.setUser(MyApplication.getInstance().userFullName);
						comment.setText(text);
						comment.setWishItem(wishItem);
						commentsAdapter.add(comment);
						commentEditText.setText("");
					}
				});
		commentsListView.setAdapter(commentsAdapter);
	}

	private void setItemInfo() {
		// TODO Auto-generated method stub
		holder = new ViewHolder();
		holder.title = (TextView) findViewById(R.id.comment_page_inner_item_title);
		holder.description = (TextView) findViewById(R.id.comment_page_inner_description);
		holder.dateCreated = (TextView) findViewById(R.id.comment_page_inner_date_created);
		holder.image = (ImageView) findViewById(R.id.comment_page_inner_imageView);
		
		holder.title.setText(wishItem.getTitle());
		holder.description.setText(wishItem.getDescription());
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		holder.dateCreated.setText(formatter.format(wishItem
				.getDateCreated()));
		if (wishItem.getImage() != null) {
		    Bitmap bmp = wishItem.getImage();
		    int width=150;
		    int height=150;
		    bmp=Bitmap.createScaledBitmap(bmp, width,height, true);
		    
			holder.image.setImageBitmap(bmp);
		}
	}

	static class CommentsAdapter extends ArrayAdapter<Comment> {

		public CommentsAdapter(Context context, List<Comment> comments) {
			super(context, 0, comments);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Comment comment = getItem(position);
			RelativeLayout commentView;
			if (convertView == null) {
				convertView = inflateCommentView();
			}
			commentView = (RelativeLayout) convertView;
			TextView user = (TextView) commentView.findViewById(R.id.user);
			TextView date = (TextView) commentView.findViewById(R.id.date);
			TextView text = (TextView) commentView.findViewById(R.id.text);
			user.setText(comment.getUser());
			Date dateCreated = comment.getDatecreated();
			if (dateCreated != null) {
				date.setText(dateCreated.toString());
			}
			text.setText(comment.getText());
			return commentView;
		}

		private RelativeLayout inflateCommentView() {
			return (RelativeLayout) LayoutInflater.from(getContext()).inflate(
					R.layout.comment_list_item, null);
		}
	}
	
	private static class ViewHolder {
		TextView title;
		TextView description;
		TextView dateCreated;
		ImageView image;
	}
}
